package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.project.RCCommissionYearConfig;
import com.shrct.businessprocess.project.RCProjectSaleAssignmentCriteria;
import com.shrct.businessprocess.project.RCProjectSaleAssignmentCriteriaSummary;
import com.shrct.businessprocess.project.RCProjectSaleAssignmentCriteriaSummaryData;
import com.shrct.businessprocess.project.RCProjectSaleAssignmentNormalCriteriaSummary;
import com.shrct.businessprocess.project.RCProjectSaleAssignmentNormalCriteriaSummaryData;
import com.shrct.businessprocess.project.RCSaleCommissionTotalSettlement;
import com.shrct.businessprocess.project.RCSaleCommissionTotalSettlementDAO;
import com.shrct.businessprocess.project.RCSalesAssignment;
import com.shrct.businessprocess.project.RCSalesCommissionYearConfig;
import com.shrct.businessprocess.project.RCSalesCommissionYearSettlement;
import com.shrct.businessprocess.project.events.RCSaleCommissionTotalSettlementEventsManager;
import com.shrct.businessprocess.project.impl.RCSaleCommissionTotalSettlementImpl;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.RCSaleCommissionTotalSettlementDAO")
public class RCSaleCommissionTotalSettlementDAOImpl extends BaseDAOService<RCSaleCommissionTotalSettlement> implements RCSaleCommissionTotalSettlementDAO {
  @Autowired
  private RCSaleCommissionTotalSettlementEventsManager eventsManager;
  
  @Autowired
  private IObjectFactory entities;
  
  public Double calcTotalContractProfit(final String year) {
    Double _xblockexpression = null;
    {
      RCProjectSaleAssignmentCriteria _createQueryCriteria = this.entities.createQueryCriteria(RCProjectSaleAssignmentCriteria.class);
      final Procedure1<RCProjectSaleAssignmentCriteria> _function = new Procedure1<RCProjectSaleAssignmentCriteria>() {
          public void apply(final RCProjectSaleAssignmentCriteria it) {
            it.setYear(year);
            it.setIsEarlyBuild(Boolean.valueOf(false));
          }
        };
      final RCProjectSaleAssignmentCriteria c = ObjectExtensions.<RCProjectSaleAssignmentCriteria>operator_doubleArrow(_createQueryCriteria, _function);
      RCProjectSaleAssignmentNormalCriteriaSummary _createStatCriteria = c.createStatCriteria(RCProjectSaleAssignmentNormalCriteriaSummary.class);
      Collection<RCProjectSaleAssignmentNormalCriteriaSummaryData> _list = _createStatCriteria.list();
      RCProjectSaleAssignmentNormalCriteriaSummaryData _head = IterableExtensions.<RCProjectSaleAssignmentNormalCriteriaSummaryData>head(_list);
      Double _contractProfit = _head.getContractProfit();
      _xblockexpression = (_contractProfit);
    }
    return _xblockexpression;
  }
  
  public RCSaleCommissionTotalSettlement doCreate(final Date settlementDate, final RCSalesAssignment saler, final Procedure1<? super RCSalesCommissionYearSettlement> initItem, final Procedure1<? super RCSaleCommissionTotalSettlement> init) {RCSaleCommissionTotalSettlement rCSaleCommissionTotalSettlement=RCSaleCommissionTotalSettlementImpl.create(getObjectFactory());
    final Procedure1<RCSaleCommissionTotalSettlement> _function = new Procedure1<RCSaleCommissionTotalSettlement>() {
        public void apply(final RCSaleCommissionTotalSettlement settlement) {
          IGenericQuery<RCSaleCommissionTotalSettlement> query=getObjectFactory().createGenericQuery(RCSaleCommissionTotalSettlement.class,"select e from com.shrct.businessprocess.project.impl.RCSaleCommissionTotalSettlementImpl as e   where ((e.saler) = (:saler)) and  e.active = 1      ").setParameter("saler",saler);
          Collection<RCSaleCommissionTotalSettlement> _list = query.list();
          final Procedure1<RCSaleCommissionTotalSettlement> _function = new Procedure1<RCSaleCommissionTotalSettlement>() {
              public void apply(final RCSaleCommissionTotalSettlement it) {getObjectFactory().delete(it);
                
              }
            };
          IterableExtensions.<RCSaleCommissionTotalSettlement>forEach(_list, _function);
          settlement.setSaler(saler);
          settlement.setDate(settlementDate);
          Collection<RCSalesCommissionYearConfig> _commissionConfigs = saler.getCommissionConfigs();
          final Procedure1<RCSalesCommissionYearConfig> _function_1 = new Procedure1<RCSalesCommissionYearConfig>() {
              public void apply(final RCSalesCommissionYearConfig config) {
                RCSalesCommissionYearSettlement _createAndAddtoYearParts = settlement.createAndAddtoYearParts();
                final Procedure1<RCSalesCommissionYearSettlement> _function = new Procedure1<RCSalesCommissionYearSettlement>() {
                    public void apply(final RCSalesCommissionYearSettlement it) {
                      it.setYearConfig(config);
                      initItem.apply(it);
                    }
                  };
                ObjectExtensions.<RCSalesCommissionYearSettlement>operator_doubleArrow(_createAndAddtoYearParts, _function);
              }
            };
          IterableExtensions.<RCSalesCommissionYearConfig>forEach(_commissionConfigs, _function_1);
          init.apply(settlement);
          RCSaleCommissionTotalSettlementDAOImpl.this.eventsManager.fireCreated(settlement);
        }
      };
    RCSaleCommissionTotalSettlement _doubleArrow = ObjectExtensions.<RCSaleCommissionTotalSettlement>operator_doubleArrow(rCSaleCommissionTotalSettlement, _function);
    return _doubleArrow;
  }
  
  public void initItem(final RCSalesCommissionYearSettlement item) {
    RCProjectSaleAssignmentCriteria _createQueryCriteria = this.entities.createQueryCriteria(RCProjectSaleAssignmentCriteria.class);
    final Procedure1<RCProjectSaleAssignmentCriteria> _function = new Procedure1<RCProjectSaleAssignmentCriteria>() {
        public void apply(final RCProjectSaleAssignmentCriteria it) {
          RCSalesCommissionYearConfig _yearConfig = item.getYearConfig();
          RCCommissionYearConfig _overallConfig = _yearConfig.getOverallConfig();
          Integer _year = _overallConfig.getYear();
          String _string = _year.toString();
          it.setYear(_string);
          RCSalesCommissionYearConfig _yearConfig_1 = item.getYearConfig();
          RCSalesAssignment _saler = _yearConfig_1.getSaler();
          RCEmployee _saler_1 = _saler.getSaler();
          it.setSales(_saler_1);
          it.setIsEarlyBuild(Boolean.valueOf(false));
        }
      };
    final RCProjectSaleAssignmentCriteria c = ObjectExtensions.<RCProjectSaleAssignmentCriteria>operator_doubleArrow(_createQueryCriteria, _function);
    RCProjectSaleAssignmentCriteriaSummary _createStatCriteria = c.createStatCriteria(RCProjectSaleAssignmentCriteriaSummary.class);
    Collection<RCProjectSaleAssignmentCriteriaSummaryData> _list = _createStatCriteria.list();
    final RCProjectSaleAssignmentCriteriaSummaryData statData = IterableExtensions.<RCProjectSaleAssignmentCriteriaSummaryData>head(_list);
    Double _contractProfitAdj4Sale = statData.getContractProfitAdj4Sale();
    Double _nullsafe = NumberExtensions.nullsafe(_contractProfitAdj4Sale);
    item.setContractProfitAdj4Sale(_nullsafe);
    Double _contractProfit = statData.getContractProfit();
    Double _nullsafe_1 = NumberExtensions.nullsafe(_contractProfit);
    item.setContractProfit(_nullsafe_1);
    Double _cggp = statData.getCggp();
    Double _nullsafe_2 = NumberExtensions.nullsafe(_cggp);
    item.setCggp(_nullsafe_2);
    RCSalesCommissionYearConfig _yearConfig = item.getYearConfig();
    RCCommissionYearConfig _overallConfig = _yearConfig.getOverallConfig();
    Integer _year = _overallConfig.getYear();
    String _string = _year.toString();
    Double _calcTotalContractProfit = this.calcTotalContractProfit(_string);
    Double _nullsafe_3 = NumberExtensions.nullsafe(_calcTotalContractProfit);
    item.setTotalContractProfit(_nullsafe_3);
  }
  
  public RCSaleCommissionTotalSettlement create(final Date settlementDate, final RCSalesAssignment saler, final Procedure1<? super RCSaleCommissionTotalSettlement> init) {
    final Procedure1<RCSalesCommissionYearSettlement> _function = new Procedure1<RCSalesCommissionYearSettlement>() {
        public void apply(final RCSalesCommissionYearSettlement it) {
          RCSaleCommissionTotalSettlementDAOImpl.this.initItem(it);
        }
      };
    final Procedure1<RCSaleCommissionTotalSettlement> _function_1 = new Procedure1<RCSaleCommissionTotalSettlement>() {
        public void apply(final RCSaleCommissionTotalSettlement it) {
          init.apply(it);
        }
      };
    RCSaleCommissionTotalSettlement _doCreate = this.doCreate(settlementDate, saler, _function, _function_1);
    return _doCreate;
  }
  
  public void delete(final RCSaleCommissionTotalSettlement value) {getObjectFactory().delete(value);
    
    this.eventsManager.fireDeleted(value);
  }
  
  public void update(final RCSaleCommissionTotalSettlement value, final Procedure1<? super RCSaleCommissionTotalSettlement> updater) {
    updater.apply(value);
    this.eventsManager.fireUpdateded(value);
  }
}
